//
//  FQDMainQButton.m
//  FakeQDaily
//
//  Created by mewxiv on 8/28/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDMainQButton.h"

static NSString *const kQIconImageName = @"c_Qdaily button";
static NSString *const kCloseImageName = @"c_close button";
static NSString *const kBackImageName = @"homeBackButton";

@interface FQDMainQButton ()

@property (nonatomic, assign) FQDMainQButtonState displayState;

@end

@implementation FQDMainQButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
        [self prepareUI];
    }
    return self;
}

- (void)commonInit
{
    _displayState = FQDMainQButtonStateClose;
}

- (void)prepareUI
{
    [self setImage:[UIImage imageNamed:kQIconImageName]
          forState:UIControlStateNormal];
}

- (void)switchIconState:(FQDMainQButtonState)displayState
{
    UIImage *iconImage = nil;
    if (displayState == FQDMainQButtonStateOpen) {
        iconImage = [UIImage imageNamed:kCloseImageName];
    } else if (displayState == FQDMainQButtonStateWillBack) {
        iconImage = [UIImage imageNamed:kBackImageName];
    } else {
        iconImage = [UIImage imageNamed:kQIconImageName];
    }
    self.displayState = displayState;
    [self setImage:iconImage forState:UIControlStateNormal];
}

- (void)playBounceAnimation
{
    CGFloat duration = 0.35f;
    CGFloat damping = 0.7f;
    CGFloat initialVelocity = 0;
    
    CGRect rectInWindow = [self.superview convertRect:self.frame toView:self.window];
    CGFloat moveY = CGRectGetHeight(self.window.frame) - CGRectGetMinY(rectInWindow);
    
    BOOL isClose = (self.displayState == FQDMainQButtonStateClose);
    FQDMainQButtonState toState = (isClose) ? FQDMainQButtonStateOpen : FQDMainQButtonStateClose;
    UIViewAnimationOptions downOptions = UIViewAnimationOptionCurveEaseIn;
    [UIView animateWithDuration:(duration / 3) delay:0 options:downOptions animations:^{
        CGAffineTransform transform = CGAffineTransformMakeTranslation(0, moveY);
        self.transform = transform;
    } completion:^(BOOL finished) {
        UIViewAnimationOptions upOptions = UIViewAnimationOptionCurveEaseOut;
        [UIView animateWithDuration:(duration * 2 / 3) delay:0 usingSpringWithDamping:damping initialSpringVelocity:initialVelocity options:upOptions animations:^{
            self.transform = CGAffineTransformIdentity;
            
            [self switchIconState:toState];
        } completion:^(BOOL finished) {
        }];
    }];
}

- (void)playRushToAnimation
{
    CGRect rectInWindow = [self.superview convertRect:self.frame toView:self.window];
    CGFloat moveX = CGRectGetWidth(self.window.frame) - CGRectGetMinX(rectInWindow);
    
    CGAffineTransform transform = CGAffineTransformMakeTranslation(moveX, 0);
    self.transform = transform;
    [self switchIconState:FQDMainQButtonStateWillBack];
    
    [UIView animateWithDuration:0.45f delay:0 usingSpringWithDamping:0.84f initialSpringVelocity:15.0f options:UIViewAnimationOptionCurveEaseOut
     animations : ^{
        self.transform = CGAffineTransformIdentity;
    } completion: ^(BOOL finished) {
    }];
}

- (void)playRushBackAnimation
{
    CGFloat screenW = CGRectGetWidth([UIScreen mainScreen].bounds);
    
    // 克隆一个按钮执行从屏幕左边以外的区域跑进屏幕右边的区域的动画
    // 因为无法直接用usingSpringWithDamping执行同一个横跨屏幕边缘来到另一边的动画，
    // 所以用两个相同的按钮分别执行两段动画的方法实现
    FQDMainQButton *cloneQButton = [[FQDMainQButton alloc] init];
    cloneQButton.frame = self.frame;
    [cloneQButton switchIconState:FQDMainQButtonStateOpen];
    [self.superview addSubview:cloneQButton];
    
    CGAffineTransform transform = CGAffineTransformMakeTranslation(-screenW, 0);
    cloneQButton.transform = transform;
    [UIView animateWithDuration:0.45f delay:0 usingSpringWithDamping:0.84f initialSpringVelocity:15.0f options:UIViewAnimationOptionCurveEaseOut
     animations : ^{
        cloneQButton.transform = CGAffineTransformIdentity;
    } completion: ^(BOOL finished) {
        [cloneQButton removeFromSuperview];
    }];

    self.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:0.45f delay:0 usingSpringWithDamping:0.84f initialSpringVelocity:15.0f options:UIViewAnimationOptionCurveEaseOut
     animations : ^{
        CGAffineTransform transform = CGAffineTransformMakeTranslation(screenW, 0);
        self.transform = transform;
    } completion: ^(BOOL finished) {
        self.transform = CGAffineTransformIdentity;
        [self switchIconState:FQDMainQButtonStateOpen];

    }];
}

@end
